package cn.rongdao.jrkabao.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import cn.mifengkong.appcommon.util.ActivityStartUtil;
import cn.rongdao.jrkabao.BuildConfig;
import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.ui.webview.WebViewActivity;


/**
 *
 */

public class DialogKaMaoActivity extends Dialog {
    public DialogKaMaoActivity(Context context) {
        super(context);
    }

    public DialogKaMaoActivity(Context context, int themeResId) {
        super(context);
    }

    protected DialogKaMaoActivity(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {

        /**
         * Helper class for creating a custom dialog
         */

        private Activity activity;
        private String title;
        private String link;
        private String imageUrl;

        public Builder(Activity activity) {
            this.activity = activity;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public DialogKaMaoActivity.Builder setTitle(int title) {
            this.title = (String) activity.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */
        public DialogKaMaoActivity.Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * Set the Dialog link from resource
         *
         * @param imageUrl
         * @return
         */
        public DialogKaMaoActivity.Builder setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
            return this;
        }

        /**
         * Set the Dialog link from resource
         *
         * @param link
         * @return
         */
        public DialogKaMaoActivity.Builder setLink(String link) {
            this.link = link;
            return this;
        }

        /**
         * Create the custom dialog
         */
        public DialogKaMaoActivity create() {
            final DialogKaMaoActivity dialog = new DialogKaMaoActivity(activity, R.style.Dialog);
            dialog.setCanceledOnTouchOutside(false);
            Window window = dialog.getWindow();
            //取消标题
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //取消状态栏
            window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            window.setBackgroundDrawable(new ColorDrawable());
            window.setContentView(R.layout.dialog_kamao_activity);

            final ImageView ivClose = window.findViewById(R.id.dka_iv_close);
            ImageView ivMain = window.findViewById(R.id.dka_iv_main);

            FRImageLoad.loadImage(FRString.valueOf(imageUrl), ivMain, new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    ivClose.setVisibility(View.VISIBLE);
                    return false;
                }
            });

            ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                }
            });

            ivMain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.cancel();
                    if (!FRString.isEmpty(link) && link.startsWith("kamao://")) {
                        ActivityStartUtil.launch(activity, BuildConfig.APPLICATION_ID, link);
                    } else {
                        WebViewActivity.start(activity, link);
                    }
                }
            });
            return dialog;
        }
    }
}
